package eleventh.Comparable接口;

import java.util.Arrays;

// 设计一个学生类 要求成绩由高到低排序 如果成绩相等，则按年龄由低到高排序。
class Student implements Comparable<Student> {// 指定类型为Student
    private String name;
    private int age;
    private float score;

    public Student(String name, int age, float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return name+"\t\t"+age+"\t\t"+score;
    }

    @Override
    public int compareTo(Student stu) { // 覆写compareTO方法
        if (this.score > stu.score) {
            return -1;
        } else if (this.score < stu.score) {
            return 1;
        } else {
            if (this.age > stu.age) {
                return 1;
            } else if (this.age < stu.age) {
                return -1;
            } else {
                return 0;
            }
        }
    }
}

public class 使用比较器进行排序操作 {
    public static void main(String[] args) {
        Student[] stu = {
                new Student("张三", 18, 88.8f),
                new Student("李四", 28, 77.7f),
                new Student("赵武", 11, 99.9f),
                new Student("丽丽", 88, 12.5f),
        };
        Arrays.sort(stu);
        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i]);
        }
    }
}
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=57318:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 eleventh.Comparable接口.使用比较器进行排序操作
赵武		11		99.9
张三		18		88.8
李四		28		77.7
丽丽		88		12.5

Process finished with exit code 0

 */